/**
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.srwl.easeui.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.text.TextUtils;

import com.srwl.mytx.domain.Message;

import java.text.DecimalFormat;

public class EaseCommonUtils {
    private static final String TAG = "CommonUtils";

    /**
     * check if network avalable
     *
     * @param context
     * @return
     */
    public static boolean isNetWorkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable() && mNetworkInfo.isConnected();
            }
        }

        return false;
    }

    /**
     * check if sdcard exist
     *
     * @return
     */
    public static boolean isSdcardExist() {
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
            return true;
        else
            return false;
    }


    static String getString(Context context, int resId) {
        return context.getResources().getString(resId);
    }


    public static String getInitialLette(String name) {
        final String DefaultLetter = "#";

        if (TextUtils.isEmpty(name)) {
            return DefaultLetter;
        }
        char char0 = name.toLowerCase().charAt(0);
        if (Character.isDigit(char0)) {
            return DefaultLetter;
        }
//        ArrayList<Token> l = HanziToPinyin.getInstance().get(name.substring(0, 1));
//        if (l != null && l.size() > 0 && l.get(0).target.length() > 0) {
//            Token token = l.get(0);
//            String letter = token.target.substring(0, 1).toUpperCase();
//            char c = letter.charAt(0);
//            if (c < 'A' || c > 'Z') {
//                return DefaultLetter;
//            }
//            return letter;
//        }
        return DefaultLetter;
    }

    public static String getDataSize(long var0) {
        DecimalFormat var2 = new DecimalFormat("###.00");
        return var0 < 0L ? "error" : (var0 < 1024L ? var0 + "bytes" : (var0 < 1048576L ? var2.format((double) ((float) var0 / 1024.0F)) + "KB" : (var0 < 1073741824L ? var2.format((double) ((float) var0 / 1024.0F / 1024.0F)) + "MB" : var2.format((double) ((float) var0 / 1024.0F / 1024.0F / 1024.0F)) + "GB")));
    }

    public static boolean isSendMessage(Message message, String myUserId) {
        return TextUtils.equals(message.getFromId(), myUserId);
    }

    /**
     * 判断文件消息的文件地址是本地还是远程的，远程的就是一个key (文件名)
     *
     * @param content
     * @return
     */
    public static boolean isLocalFilePath(String content) {
        return content.startsWith(Environment.getExternalStorageDirectory().getAbsolutePath());
    }


}
